package com.bfg.model;

import java.util.HashMap;
import java.util.ResourceBundle;
import javax.servlet.http.*;

/**
 * Holds the information about the currently logged in customer.
 * <P>
 * 
 * Copyright 2002, James M. Turner No warranty is made as to the fitness of this
 * code for any particular purpose. It is included for demonstration purposes
 * only.
 * 
 * @author James M. Turner
 */

public class Customer implements HttpSessionBindingListener {

	private String last_name;
	private String first_name;
	private String email;
	private String password;
	private HashMap addresses = new HashMap();;
	private HashMap wallet = new HashMap();
	private int customer_id;

	private static ResourceBundle sql_bundle = ResourceBundle
			.getBundle("com.bfg.model.SQLQueries");

	public String getLast_name() {
		return last_name;
	}

	public void setLast_name(String lastName) {
		last_name = lastName;
	}

	public String getFirst_name() {
		return first_name;
	}

	public void setFirst_name(String firstName) {
		first_name = firstName;
	}

	/**
	 * Returns the email address of this customer.
	 * 
	 * @return The email address
	 **/

	public String getEmail() {
		return email;
	}

	/**
	 * Sets the email address of this customer.
	 * 
	 * @param em
	 *            The email address
	 **/

	public void setEmail(String em) {
		email = em;
	}

	/**
	 * Gets the password of this customer.
	 * 
	 * @return The password as a String
	 **/

	public String getPassword() {
		return password;
	}

	/**
	 * Sets the password of this customer.
	 * 
	 * @param pw
	 *            The password as a String
	 **/

	public void setPassword(String pw) {
		password = pw;
	}

	/**
	 * Returns the unique customer ID, which is an autoincrementing database
	 * column used to uniquely identify each customer record.
	 * 
	 * @return the customer ID
	 **/

	public int getCustomerId() {
		return customer_id;
	}

	/**
	 * Sets the unique customer ID, which is an autoincrementing database column
	 * used to uniquely identify each customer record. Note that in normal
	 * usage, this should not be used because IDs are assigned by the database.
	 * 
	 * @param id
	 *            the customer ID
	 **/

	public void setCustomerId(int id) {
		customer_id = id;
	}

	/**
	 * Returns a HashMap containing all the credit cards in the customer's
	 * wallet, with the key being the card id number.
	 * 
	 * @return A HashMap containing CreditCard objects
	 **/

	public HashMap getWallet() {
		return wallet;
	}

	/**
	 * Returns a HashMap containing all the addresses in the customer's address
	 * book, with the key being the address id.
	 * 
	 * @return A HashMap containing Address objects
	 **/

	public HashMap getAddressBook() {
		return addresses;
	}

	private HashMap validationErrors = new HashMap();

	/**
	 * Returns a validation error against a specific field. If a field was found
	 * to have an error during {@link #validateCustomer validateCustomer}, the
	 * error message can be accessed via this method.
	 * 
	 * @param fieldname
	 *            The bean property name of the field
	 * @return The error message for the field or null if none is present.
	 **/

	public String getFieldError(String fieldname) {
		return ((String) validationErrors.get(fieldname));
	}

	/**
	 * Sets the validation error against a specific field. Used by
	 * {@link #validateCustomer validateCustomer}.
	 * 
	 * @param fieldname
	 *            The bean property name of the field
	 * @param error
	 *            The error message for the field or null if none is present.
	 **/

	public void addFieldError(String fieldname, String error) {
		validationErrors.put(fieldname, error);
	}

	/**
	 * Validates the customer for missing or invalid fields.
	 * 
	 * @return <code>true</code> if the card passes the validation checks,
	 *         otherwise <code>false</code>
	 **/

	public boolean validateCustomer() {
		validationErrors.clear();
		boolean valid = true;
		if ((email == null) || (email.length() == 0)) {
			addFieldError("email", "e-Mail Address is required.");
			valid = false;
		} else {
			if (email.indexOf("@") == -1) {
				addFieldError("email", "Please supply a valid address.");
				valid = false;
			}
		}
		if ((password == null) || (password.length() == 0)) {
			addFieldError("password", "Password is required.");
			valid = false;
		}
		return valid;
	}

	@Override
	public void valueBound(HttpSessionBindingEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void valueUnbound(HttpSessionBindingEvent arg0) {
		// TODO Auto-generated method stub

	}

}
